package com.icinfo.cloud.provider.punish.ucase.punishexecute.controller;

import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.punish.ucase.casemain.dto.CaseDetailInfoQueryDto;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICaseDocumentRecordService;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICaseMainDetailInfoService;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICaseMainInfoService;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.CaseDetailInfoVo;
import com.icinfo.cloud.provider.punish.ucase.punishexecute.dto.CasePunishActionDto;
import com.icinfo.cloud.provider.punish.ucase.punishexecute.dto.EnforceExecuteApplyInfoQueryDto;
import com.icinfo.cloud.provider.punish.ucase.punishexecute.dto.EnforceExecuteApplyInfoSaveDto;
import com.icinfo.cloud.provider.punish.ucase.punishexecute.service.ICaseExecuteService;
import com.icinfo.cloud.provider.punish.ucase.punishexecute.vo.EnforceExecuteApplyInfoVo;
import com.icinfo.cloud.provider.punish.ucase.punishinform.dto.ExecuteFinishInfoQueryDto;
import com.icinfo.cloud.provider.punish.ucase.punishinform.vo.CaseExecuteInfoVo;
import com.icinfo.cloud.provider.punish.ucase.punishinform.vo.ExecuteFinishInfoVo;
import com.icinfo.framework.common.web.Result;
import com.icinfo.framework.core.web.BaseController;
import com.icinfo.framework.tools.utils.StringUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 处罚执行 controller
 * @author Administrator
 */
@RestController
@RequestMapping("/execute")
public class CaseExecuteController extends BaseController {

    @Resource
    private ICaseDocumentRecordService caseDocumentRecordService;

    @Resource
    private ICaseMainInfoService caseMainInfoService;

    @Resource
    private ICaseMainDetailInfoService caseMainDetailInfoService;
    @Resource
    private ICaseExecuteService caseExecuteService;


    /**
     * 描述：案件处罚执行完毕
     *
     * @param casePunishActionDto {@link CasePunishActionDto}
     * @return Result 操作结果
     * @author xiangyuyu
     * @date 2022-04-29 14:45
     */
    @PostMapping("/punishAction")
    @ApiOperation(value = "【V1.1.0】描述：案件处罚执行完毕", httpMethod = "POST")
    public Result punishAction(@Validated @RequestBody CasePunishActionDto casePunishActionDto) {
        if (!Constant.STRING_1.equals(casePunishActionDto.getRequestSource()) && StringUtils.isBlank(casePunishActionDto.getPunishActionDesc())) {
            return Result.error("描述必传！");
        }
        // 存入宽表
        CaseDetailInfoQueryDto caseDetailInfoQueryDto = new CaseDetailInfoQueryDto();
        caseDetailInfoQueryDto.setCaseId(casePunishActionDto.getCaseId());
        CaseDetailInfoVo caseDetailInfo = caseMainInfoService.getCaseDetailInfo(caseDetailInfoQueryDto);
        String punishKinds = caseDetailInfo.getPunishKinds();
        System.out.println("__________________________________________________-" + punishKinds);
        if (StringUtils.isNotBlank(punishKinds) && !Constant.STRING_1.equals(casePunishActionDto.getRequestSource())) {
            System.out.println("__________________________________________________-" + punishKinds);
            if (punishKinds.contains("罚款") || punishKinds.contains("没收违法所得")) {
                System.out.println("__________________________________________________-" + punishKinds);
                if (ObjectUtils.isEmpty(casePunishActionDto.getPaymentProofFileList())) {
                    return Result.error("请上传缴款凭证！");
                }
            }
        }
        casePunishActionDto.setCaseStageCode(caseDetailInfo.getCaseCurrentStageCode());
        if (caseExecuteService.savePunishExecuteInfo(casePunishActionDto).size() > 0) {
            // 是否更新案件状态，查询配置表
            // todo
            return Result.success("success");
        }
        return Result.error("fail");
    }

    /**
     * 【V1.1.0】描述：保存强制执行申请信息
     *
     * @param saveDto {@link EnforceExecuteApplyInfoSaveDto}
     * @return {@link Result<String>} 保存结果
     * @author xiangyuyu
     * @date 2022-04-09 11:57
     */
    @PostMapping("/saveEnforceExecuteApplyInfo")
    @ApiOperation(value = "【V1.1.0】描述：保存强制执行申请信息", httpMethod = "POST")
    public Result<String> saveEnforceExecuteApplyInfo(@RequestBody @Validated EnforceExecuteApplyInfoSaveDto saveDto) {
        return caseExecuteService.saveEnforceExecuteApplyInfo(saveDto);
    }

    /**
     * 【V1.1.0】描述：获取强制执行申请信息
     *
     * @param queryDto {@link EnforceExecuteApplyInfoQueryDto}
     * @return {@link Result<EnforceExecuteApplyInfoVo>} 强制执行申请信息
     * @author xiangyuyu
     * @date 2022-04-09 11:57
     */
    @PostMapping("/getEnforceExecuteApplyInfo")
    @ApiOperation(value = "【V1.1.0】描述：获取强制执行申请信息", httpMethod = "POST")
    public Result<EnforceExecuteApplyInfoVo> getEnforceExecuteApplyInfo(@RequestBody @Validated EnforceExecuteApplyInfoQueryDto queryDto) {
        EnforceExecuteApplyInfoVo vo = caseExecuteService.getEnforceExecuteApplyInfo(queryDto);
        return Result.success(vo);
    }


    /**
     * 【V1.1.0】描述：获取执行完成情况信息
     *
     * @param queryDto {@link ExecuteFinishInfoQueryDto}
     * @return {@link Result<ExecuteFinishInfoVo>} 执行完成情况信息
     * @author xiangyuyu
     * @date 2022-04-09 11:57
     */
    @PostMapping("/getExecuteFinishInfo")
    @ApiOperation(value = "【V1.1.0】描述：获取执行完成情况信息", httpMethod = "POST")
    public Result<ExecuteFinishInfoVo> getExecuteFinishInfo(@RequestBody @Validated ExecuteFinishInfoQueryDto queryDto) {
        ExecuteFinishInfoVo vo = caseExecuteService.getExecuteFinishInfo(queryDto);
        return Result.success(vo);
    }

    /**
     * 【V2.0.0】描述：重新构造执行情况文书内容
     *
     * @return {@link Result<String>} 操作结果
     * @author xiangyuyu
     * @date 2022-05-18 10:07
     */
    //@PostMapping("/refactorExecuteConditionDoc")
    @ApiOperation(value = "【V2.0.0】描述：重新构造执行情况文书内容", httpMethod = "POST")
    public Result<String> refactorExecuteConditionDoc() {
        return caseExecuteService.refactorExecuteConditionDoc();
    }


    @PostMapping("/getExecuteInfoByCaseId")
    public Result<CaseExecuteInfoVo> getExecuteInfoByCaseId(@RequestBody @Validated ExecuteFinishInfoQueryDto queryDto){
        try {
            return Result.success(caseExecuteService.getExecuteInfoByCaseId(queryDto.getCaseId()));
        }catch (Exception e){
            return Result.error(500,"错误：" + e.getMessage());
        }
    }

}
